package com.le.bbs.biz;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.le.bbs.dao.ReplyDao;
import com.le.bean.Reply;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional( // 有关事务处理的注解
        propagation = Propagation.SUPPORTS, // 传播行为，这里是有事务就用事务，没有也可以运行
        isolation = Isolation.DEFAULT, // 隔离级别，这里是默认
        timeout = 2000, // 超时时间，表示业务执行超过多少就会自动回滚，这里是2秒
        readOnly = true, // 只读设置，表示这个业务只进行查询操作
        rollbackFor = RuntimeException.class
)
@Slf4j
public class BbsReplyBizImpl implements BbsReplyBiz{
    @Autowired
    private ReplyDao replyDao;

    @Override
    public List<Reply> findAll(int topicid) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("topicid", topicid);
        wrapper.orderByAsc("replyid"); // 因为Reply的id是自增的，自然id愈大评论就越往后
        return this.replyDao.selectList(wrapper);
    }

    @Override
    public Reply findEnd(int topicid) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("topicid", topicid);
        wrapper.orderByDesc("replyid"); // 因为Reply的id是自增的，自然id愈大评论就越往后
        wrapper.last("LIMIT 1");
        return this.replyDao.selectOne(wrapper);
    }
}
